<?php


class User
{
   function __construct($db,$req)
   {
       $this->db = $db;
       $this->req = $req;
   }
   function json($sta,$data){
       return json_encode(array('status'=>$sta,'msg'=>$data));
   }
   function login(){
      @ $name = $this->req['name'];
      @ $password = $this->req['password'];
      @ $time = $this->req['time'];
      @ $address = $this->req['address'];
       if(empty($name)||empty($password)){
           exit($this->json(500,'名字或者密码不能为空'));
       }else{
         $r = $this->db->query('select * from user where name=?&&password=?',$name,$password);
         if(!empty($r)){
             $this->db->query("update user set preTime=?,preAddress=? where name=?",$time,$address,$name);
             echo $this->json(200,'登陆成功');
         }else{
             echo $this->json(500,'不存在该用户');
         }

       }
   }
   function findInfo(){
       @ $name = $this->req['name'];
       if(empty($name)){
           exit($this->json(500,'名字不能为空'));
       }else{
           $r = $this->db->query('select * from user where name=?',$name);
           echo $this->json(200,$r);
       }
   }
   function addAsk(){
       $r = $this->db->query('select * from user');
       $count = (int)$r[0]['askCount']+1;
       $id = (int)$r[0]['id'];
       $this->db->query('update user set askCount=? where id=?',$count,$id);
       echo $this->json(200,'增加访问量成功');
   }
}